package com.zzp.fastdfs.file.controller;

import com.zzp.fastdfs.file.service.FastDFSService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 佐斯特勒
 * <p>
 * 人皆知有用之用，而莫知无用之用也
 * </p>
 * @version v1.0.0
 * @date 2020/1/28 20:30
 * @see UploadController
 **/
@RequestMapping("/upload")
@RestController
@Slf4j
public class UploadController {

    @Resource
    private FastDFSService fastDFSService;

    private Map<String, Object> map = new HashMap<>();

    @PostMapping("/")
    public Object uploadFile(MultipartFile file, HttpServletRequest req) {
        var uri = fastDFSService.uploadFile(file, req);
        map.put("path",uri);
        return map;
    }

    @PostMapping("/thumb")
    public List<String> uploadThumbFile(MultipartFile file, HttpServletRequest req){
        return Arrays.asList(fastDFSService.uploadThumbImage(file, req));
    }

    @GetMapping("/downLoad")
    public Object downLoad(String fileUrl, HttpServletResponse resp){
        try {
            fastDFSService.downloadFile(fileUrl, resp);
        } catch (IOException e) {
            log.error("下载异常");
            log.warn(e.getMessage());
            return "fail";
        }
        return "success";
    }
}
